package com.diandi.signature.service.impl.contract;

import com.diandi.signature.bean.contract.SignatureContract;
import com.diandi.signature.contract.ContractDataService;
import com.diandi.signature.manager.contract.ContractDataManager;
import com.diandi.signature.url.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mxp on 2017/12/15.
 */
@Service
public class ContractDataServiceImpl implements ContractDataService {
    private Logger logger = LoggerFactory.getLogger(ContractDataServiceImpl.class);

    @Autowired
    private ContractDataManager contractDataManager;

    @Override
    public Map<String, Object> queryList(String userId, String contractCode, String signTimeStart, String signTimeEnd, Integer page, Integer rows) {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        Map<String, Object> returnMap = new HashMap<String, Object>();
        int from = (page - 1) * rows;
        paramMap.put("from", from);
        paramMap.put("rows", rows);

        if (StringUtils.isNotBlank(userId)){
            paramMap.put("userId", userId);
        }

        if (StringUtils.isNotBlank(contractCode)){
            paramMap.put("contractCode", contractCode);
        }

        if (StringUtils.isNotBlank(signTimeStart)){
            paramMap.put("signTimeStart", DateUtil.reverseDate(signTimeStart));
        }

        if (StringUtils.isNotBlank(signTimeEnd)){
            paramMap.put("signTimeEnd", DateUtil.reverseDate(signTimeEnd));
        }

        List<SignatureContract> contractList = contractDataManager.queryList(paramMap);
        Integer count = contractDataManager.queryCount(paramMap);

        returnMap.put("total", count);
        returnMap.put("rows", contractList);

        return returnMap;
    }
}
